" commands:
" ALELint
" ALEFix
" ALEFixSuggest
" ALEDocumentation
"
let g:ale_python_autoflake_executable = 'autoflake'
let g:ale_use_global_executables = get(g:, 'ale_use_global_executables', 1)
let g:ale_linters = { 
            \   'python': ['flake8', 'pylint', 'ruff'],
            \ }

let g:ale_python_flake8_options = '-m flake8 --max-line-length=248' 

function! plugin#ale#load() abort
    if !exists('g:loaded_ale_dont_use_this_in_other_plugins_please') && has('python3')
        packadd ale
    endif
    return exists('g:loaded_ale_dont_use_this_in_other_plugins_please')
endfunction

function! plugin#ale#init() abort
    call plugin#ale#airline()
    call plugin#ale#variables()
    call plugin#ale#keymap()
    nmap <silent> <C-k> <Plug>(ale_previous_wrap)
    nmap <silent> <C-j> <Plug>(ale_next_wrap)
endfunction

function! plugin#ale#airline() abort
    " let g:airline_extensions = add(g:airline_extensions, 'ale')
    let g:airline#extensions#ale#enabled = 0
    " 错误图标默认为E
    let g:airline#extensions#ale#error_symbol = get(g:, 'ale_echo_msg_error_str', 'E:')
    " 警告图标默认为W
    let g:airline#extensions#ale#warning_symbol = get(g:, 'ale_echo_msg_warning_str', 'W:')
    " 等待图标默认为...
    let g:airline#extensions#ale#checking_symbol = '...'
endfunction

function! plugin#ale#keymap() abort
    let g:ale_keymap = get(g:, 'ale_keymap', {    'name'  : '+ale',
                \           'p'     : ['<Plug>(ale_previous_wrap)'   , 'previous' ],
                \           'n'     : ['<Plug>(ale_next_wrap)'       , 'next'     ],
                \           't'     : [':ALEToggle'                  , 'toggle'   ],
                \           'd'     : [':ALEDetail'                  , 'detail'   ],
                \           'c'     : [':ALEDocumentation'           , 'document' ],
                \           'i'     : [':ALEInfo'                    , 'info'     ],
                \ })
    let g:leader_keymap = get(g:, 'leader_keymap', { 'name': '+leader' })
    let g:leader_keymap.a = get(g:leader_keymap, 'a', get(g:, 'async_keymap', { 'name': '+async' }))
    let g:leader_keymap.a.l = g:ale_keymap
endfunction

function! plugin#ale#variables() abort
    " 保持侧边栏可见：
    let g:ale_sign_column_always = 1
    " 自定义error和warning图标
    let g:ale_sign_error = '✗'
    let g:ale_sign_warning = '⚡'
    " 改变状态栏信息格式：
    let g:ale_statusline_format = ['⨉ %d', '⚠ %d', '⬥ ok']
    " 改变命令行消息：
    let g:ale_echo_msg_error_str = 'E'
    let g:ale_echo_msg_warning_str = 'W'
    let g:ale_echo_msg_format = '[%linter%] %s [%severity%]'
    " 关闭高亮提示
    let g:ale_set_highlights = 1
    let g:ale_lint_on_enter = 1
    let g:ale_lint_on_save = 1
    let g:ale_lint_on_insert_leave = 1

    " let g:ale_root 
    " let g:ale_lint_on_text_changed
    " let g:ale_lint_on_filetype_change
    " let g:ale_echo_cursor

    " By updating loclist. (On by default)             - 
    " let g:ale_set_loclist
    " By updating quickfix. (Off by default)           - 
    " let g:ale_set_quickfix
    " By creating signs in the sign column.            - 
    " let g:ale_set_signs
    " By echoing messages based on your cursor.        - 
    " let g:ale_echo_cursor
    " By inline text based on your cursor.             - 
    " let g:ale_virtualtext_cursor
    " By displaying the preview based on your cursor.  - 
    " let g:ale_cursor_detail
    " By showing balloons for your mouse cursor        - 
    " let g:ale_set_balloons

    " Most settings can be configured for each buffer. (|b:| instead of |g:|),
    " including disabling ALE for certain buffers with |b:ale_enabled|. The
    " |g:ale_pattern_options| setting can be used to configure files differently
    " based on regular expressions for filenames. For configuring entire projects,
    " the buffer-local options can be used with external plugins for reading Vim
    " project configuration files. Buffer-local settings can also be used in
    " ftplugin files for different filetypes.
    " let b:ale_enabled
    " let g:ale_pattern_options

    " ALE offers several options for controlling which linters are run.

    " * Selecting linters to run.            - 
    " let g:ale_linters
    " * Aliasing filetypes for linters       - 
    " let g:ale_linter_aliases
    " * Only running linters you asked for.  - 
    " let g:ale_linters_explicit
    " * Disabling only a subset of linters.  - 
    " let g:ale_linters_ignore
    " * Disabling LSP linters and `tsserver`.  - 
    " let g:ale_disable_lsp

    " let g:ale_command_wrapper 
    " let g:ale_use_global_executables
    " let g:ale_filename_mappings 
endfunction
